window.onload = function() {

    //取得长度
    function getLength(str) {
        return str.replace(/[^\x00-xff]/g, "xx").length;
    }
    /* 判断是否是相同字符 */
    function findStr(str, n) {
        var tmp = 0;
        for (var i = 0; i < str.length; i++) {
            if (str.charAt(i) == n) {
                tmp++;
            }
        }
        return tmp;
    }

    var name = document.getElementById("name");
    var username = document.getElementById("username");
    var password = document.getElementById("password");
    var repassword = document.getElementById("repassword");
    var phone = document.getElementById("phone");
    var answer1 = document.getElementById("answer1");
    var answer2 = document.getElementById("answer2");
    var verification = document.getElementById("verification");
    var agree = document.getElementById("agree");
    var submit = document.getElementById("Submit");

    var aP = document.getElementsByClassName('msg');
    var username_msg = aP[0];
    var password_msg = aP[1];
    var repassword_msg = aP[2];
    var name_msg = aP[3];
    var phone_msg = aP[4];
    var answer1_msg = aP[6];
    var answer2_msg = aP[8];

    //正则判断非法字符
    var re = /[^\w\u4e00-\u9fa5]/g;

    //用户名
    username.onfocus = function() {
        username_msg.style.display = "block";
        username_msg.innerHTML = '<i>5-16位字母和数字</i>';
    }
    username.onblur = function() {
        username_msg.style.display = "block";
        username_msg.innerHTML = '<i>&nbsp</i>';
        username_len = getLength(this.value);
        if (this.value == '' || this.value == null) {
            username_msg.innerHTML = '<i class="err">不能为空</i>';
        } else if (username_len < 5) {
            username_msg.innerHTML = '<i class="err">用户名小于5个字符</i>';
        } else if (username_len > 16) {
            username_msg.innerHTML = '<i class="err">用户名大于16个字符</i>';
        } else if (re.test(this.value)) {
            username_msg.innerHTML = '<i class="err">含有非法字符</i>';
        }
    }

    //密码
    password.onfocus = function() {
        password_msg.style.display = "block";
        password_msg.innerHTML = '<i>6-16位字母和数字</i>';
    }
    password.onblur = function() {
        password_msg.style.display = "block";
        password_msg.innerHTML = '<i>&nbsp</i>';
        password_len = getLength(this.value);
        if (this.value == '' || this.value == null) {
            password_msg.innerHTML = '<i class="err">不能为空</i>';
        } else if (password_len < 6) {
            password_msg.innerHTML = '<i class="err">密码小于6个字符</i>';
        } else if (password_len > 16) {
            password_msg.innerHTML = '<i class="err">密码大于16个字符</i>';
        }
    }

    //确认密码
    repassword.onfocus = function() {
        repassword_msg.style.display = "block";
        repassword_msg.innerHTML = '<i>请再次输入密码</i>';
    }
    repassword.onblur = function() {
        repassword_msg.style.display = "block";
        repassword_msg.innerHTML = '<i>&nbsp</i>';
        if (this.value != password.value) {
            repassword_msg.innerHTML = '<i class="err">两次密码不相同</i>';
        } else if (this.value == '' || this.value == null) {
            repassword_msg.innerHTML = '<i class="err">不能为空</i>';
        }
    }

    //昵称
    name.onfocus = function() {
        name_msg.style.display = "block";
        name_msg.innerHTML = '<i>2-12位字符</i>';
    }
    name.onblur = function() {
        name_msg.style.display = "block";
        name_msg.innerHTML = '<i>&nbsp</i>';
        name_len = getLength(this.value);
        if (this.value == '' || this.value == null) {
            name_msg.innerHTML = '<i class="err">不能为空</i>';
        } else if (password_len < 2) {
            name_msg.innerHTML = '<i class="err">昵称小于2个字符</i>';
        } else if (password_len > 12) {
            name_msg.innerHTML = '<i class="err">昵称大于12个字符</i>';
        }
    }

    //电话
    phone.onfocus = function() {
        phone_msg.style.display = "block";
        phone_msg.innerHTML = '<i>11位电话号码</i>';
    }
    phone.onblur = function() {
        phone_msg.style.display = "block";
        phone_msg.innerHTML = '<i>&nbsp</i>';
        var phtest = /^1[34578]\d{9}$/;
        if (this.value == '' || this.value == null) {
            phone_msg.innerHTML = '<i class="err">不能为空</i>';
        } else if (!(phtest.test(this.value))) {
            phone_msg.innerHTML = '<i class="err">手机号码输入不正确</i>';
        }
    }

    //密保答案1
    answer1.onfocus = function() {
        answer1_msg.style.display = "block";
        answer1_msg.innerHTML = '<i>2-10个字符</i>';
    }
    answer1.onblur = function() {
        answer1_msg.style.display = "block";
        answer1_msg.innerHTML = '<i>&nbsp</i>';
        answer1_len = getLength(this.value);
        if (this.value == '' || this.value == null) {
            answer1_msg.innerHTML = '<i class="err">不能为空</i>';
        } else if (password_len < 2) {
            answer1_msg.innerHTML = '<i class="err">昵称小于2个字符</i>';
        } else if (password_len > 10) {
            answer1_msg.innerHTML = '<i class="err">昵称大于10个字符</i>';
        }
    }

    //密保答案2
    answer2.onfocus = function() {
        answer2_msg.style.display = "block";
        answer2_msg.innerHTML = '<i>2-10个字符</i>';
    }
    answer2.onblur = function() {
        answer2_msg.style.display = "block";
        answer2_msg.innerHTML = '<i>&nbsp</i>';
        answer2_len = getLength(this.value);
        if (this.value == '' || this.value == null) {
            answer2_msg.innerHTML = '<i class="err">不能为空</i>';
        } else if (password_len < 2) {
            answer2_msg.innerHTML = '<i class="err">昵称小于2个字符</i>';
        } else if (password_len > 10) {
            answer2_msg.innerHTML = '<i class="err">昵称大于10个字符</i>';
        }
    }

    //验证码

    //协议
    agree.onclick = function() {
        submit.disabled = false;
    }

    //验证
    submit.onclick = function(form) {
        if (username.value == '' || username.value == null) {
            username_msg.innerHTML = '<i class="err">不能为空</i>';
            return false;
        } else if (password.value == '' || password.value == null) {
            password_msg.innerHTML = '<i class="err">不能为空</i>';
            return false;
        } else if (repassword.value == '' || repassword.value == null) {
            repassword_msg.innerHTML = '<i class="err">不能为空</i>';
            return false;
        } else if (name.value == '' || name.value == null) {
            name_msg.innerHTML = '<i class="err">不能为空</i>';
            return false;
        } else if (phone.value == '' || phone.value == null) {
            phone_msg.innerHTML = '<i class="err">不能为空</i>';
            return false;
        } else if (answer1.value == '' || answer1.value == null) {
            answer1_msg.innerHTML = '<i class="err">不能为空</i>';
            return false;
        } else if (answer2.value == '' || answer2.value == null) {
            answer2_msg.innerHTML = '<i class="err">不能为空</i>';
            return false;
        } else {
            return true;
        }
    }
    unlogin();

}